

  // 23. 合并K个升序链表
  //!five
  function mergeKLists_Five(lists: Array < ListNode | null > ): ListNode | null {
        let p:Array<ListNode | null> = lists.map(v=>v)
        const ans:ListNode = new ListNode(-1)
        ans.next = null
        let ph = ans
        let nNull = p.reduce((pre,cur)=>cur==null?pre+1:pre,0)
        while(nNull<lists.length){
            let min = Number.MAX_VALUE
            let pMin = -1
            p.forEach((v,i)=>{
                if(v!=null){
                    if(v.val<min){
                        min = v.val
                        pMin = i
                    }
                }
            })
            ph.next = p[pMin]
            ph = ph.next as ListNode
            p[pMin] = p[pMin]?.next as ListNode| null      
            if(p[pMin]==null) nNull++     
        }
        ph.next = null
        return ans.next
  };

//   const l11 = new ListNode(1)
//   const l12 = new ListNode(4)
//   const l13 = new ListNode(5)
//   l11.next = l12
//   l12.next = l13
//   l13.next = null

//   const l21 = new ListNode(1)
//   const l22 = new ListNode(3)
//   const l23 = new ListNode(4)
//   l21.next = l22
//   l22.next = l23
//   l23.next = null

//   const l31 = new ListNode(2)
//   const l32 = new ListNode(6)
//   l31.next = l32
//   l32.next = null

//   const input = [null]


//小跟堆
class Heap_five{

    que:number[]
    len:number
    constructor(array:number[]){
        this.que = []
        this.que.push(Number.MIN_VALUE)
        this.que.push(...array)
        this.len=array.length
        this.createHeap()
    }
    createHeap(){
        for(let i= Math.floor(this.len/2);i>0;i--){
            this.justify(i)
        }
    }
    printHeap(){
        this.que.forEach(v=>console.log(v))
    }
    justify(i:number){
        if(i*2<this.len&&this.que[i]>this.que[i*2]){
            [this.que[i],this.que[i*2]] = [this.que[i*2],this.que[i]]
            this.justify(i*2)
        }
        if(i*2+1<this.len&&this.que[i]>this.que[i*2+1]){
            [this.que[i],this.que[i*2+1]] = [this.que[i*2+1],this.que[i]]
            this.justify(i*2+1)
        }
    }
    pop():number{
        // [this.que[1],this.que[this.len-1]]= [this.que[this.len-1],this.que[1]]
        // this.len--
        return this.que[1]
    }
    push(n:number){
        this.que[1] = n
        this.justify(1)
    }
}


//*小跟堆实现没写
/**
 * n : 总的节点个数
 * k ：数组里链表的个数
 * 
 * 直接循环遍历 kn
 * 使用小跟堆找最小值 lg（k）n
 * 
 */
